<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号（非主键id）" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder="订单号（非主键id）"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="订单总价格" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="订单总价格"></el-input>
    </el-form-item>
    <el-form-item label="收货人姓名快照" prop="receiverName">
      <el-input v-model="dataForm.receiverName" placeholder="收货人姓名快照"></el-input>
    </el-form-item>
    <el-form-item label="收货人手机号快照" prop="receiverMobile">
      <el-input v-model="dataForm.receiverMobile" placeholder="收货人手机号快照"></el-input>
    </el-form-item>
    <el-form-item label="收货地址快照" prop="receiverAddress">
      <el-input v-model="dataForm.receiverAddress" placeholder="收货地址快照"></el-input>
    </el-form-item>
    <el-form-item label="订单状态: 0用户已取消，10未付款（初始状态），20已付款，30已发货，40交易完成" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态: 0用户已取消，10未付款（初始状态），20已付款，30已发货，40交易完成"></el-input>
    </el-form-item>
    <el-form-item label="运费，默认为0" prop="postage">
      <el-input v-model="dataForm.postage" placeholder="运费，默认为0"></el-input>
    </el-form-item>
    <el-form-item label="支付类型,1-在线支付" prop="paymentType">
      <el-input v-model="dataForm.paymentType" placeholder="支付类型,1-在线支付"></el-input>
    </el-form-item>
    <el-form-item label="发货时间" prop="deliveryTime">
      <el-input v-model="dataForm.deliveryTime" placeholder="发货时间"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="交易完成时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="交易完成时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderNo: '',
          userId: '',
          totalPrice: '',
          receiverName: '',
          receiverMobile: '',
          receiverAddress: '',
          orderStatus: '',
          postage: '',
          paymentType: '',
          deliveryTime: '',
          payTime: '',
          endTime: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          orderNo: [
            { required: true, message: '订单号（非主键id）不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '订单总价格不能为空', trigger: 'blur' }
          ],
          receiverName: [
            { required: true, message: '收货人姓名快照不能为空', trigger: 'blur' }
          ],
          receiverMobile: [
            { required: true, message: '收货人手机号快照不能为空', trigger: 'blur' }
          ],
          receiverAddress: [
            { required: true, message: '收货地址快照不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态: 0用户已取消，10未付款（初始状态），20已付款，30已发货，40交易完成不能为空', trigger: 'blur' }
          ],
          postage: [
            { required: true, message: '运费，默认为0不能为空', trigger: 'blur' }
          ],
          paymentType: [
            { required: true, message: '支付类型,1-在线支付不能为空', trigger: 'blur' }
          ],
          deliveryTime: [
            { required: true, message: '发货时间不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '交易完成时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mall/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNo = data.order.orderNo
                this.dataForm.userId = data.order.userId
                this.dataForm.totalPrice = data.order.totalPrice
                this.dataForm.receiverName = data.order.receiverName
                this.dataForm.receiverMobile = data.order.receiverMobile
                this.dataForm.receiverAddress = data.order.receiverAddress
                this.dataForm.orderStatus = data.order.orderStatus
                this.dataForm.postage = data.order.postage
                this.dataForm.paymentType = data.order.paymentType
                this.dataForm.deliveryTime = data.order.deliveryTime
                this.dataForm.payTime = data.order.payTime
                this.dataForm.endTime = data.order.endTime
                this.dataForm.createTime = data.order.createTime
                this.dataForm.updateTime = data.order.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mall/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderNo': this.dataForm.orderNo,
                'userId': this.dataForm.userId,
                'totalPrice': this.dataForm.totalPrice,
                'receiverName': this.dataForm.receiverName,
                'receiverMobile': this.dataForm.receiverMobile,
                'receiverAddress': this.dataForm.receiverAddress,
                'orderStatus': this.dataForm.orderStatus,
                'postage': this.dataForm.postage,
                'paymentType': this.dataForm.paymentType,
                'deliveryTime': this.dataForm.deliveryTime,
                'payTime': this.dataForm.payTime,
                'endTime': this.dataForm.endTime,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
